home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’87 / Source ƒ.sit / Source ƒ / C ƒ / TRANS-LSC / TransSkel.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-12-02  |  31.1 KB  |  1,376 lines  |  [TEXT/KAHL]

  1. /*
  2.     TransSkel version 1.02 - Transportable application skeleton
  3.     
  4.     TransSkel is public domain and is written by:
  5.  
  6.             Paul DuBois
  7.             Wisconsin Regional Primate Research Center
  8.             1220 Capital Court
  9.             Madison WI  53706  USA
  10.  
  11.     UUCP:    allegra,ihnp4,seismo}!uwvax!uwmacc!dubois
  12.     ARPA:    dubois@unix.macc.wisc.edu
  13.             dubois@rhesus.primate.wisc.edu
  14.  
  15.     This version of TransSkel written for LightspeedC.  LightspeedC is a
  16.     trademark of:
  17.             THINK Technologies, Inc
  18.             420 Bedford Street  Suite 350
  19.             Lexington, MA  02173  USA
  20.  
  21.   History
  22.   06/13/86    Beta version.
  23.   08/27/86    Version number changed to 1.01.
  24.               v1.0 DoGrow bug fixed - the port at the point of the
  25.               InvalRect could have been anything; the fix is to set
  26.               the port to the grown window first.  This also explains
  27.               why the kludge to DoActivate in v1.0 worked.
  28.   10/02/86    Version number changed to 1.02, as a result of adding
  29.             modifications by David W. Berry (well!dwb@lll-lcc.arpa)
  30.             for supporting window zooming.  Also used his modifications
  31.             for supporting modeless dialogs (though not in the same
  32.             form).  Dialogs can be #define'd on or off.
  33. */
  34.  
  35.  
  36. /*
  37.     The following symbol controls support for dialogs.
  38.     Changing #define to #undef disables the support.
  39. */
  40.  
  41. # define    supportDialogs
  42.  
  43.  
  44. # ifdef        supportDialogs
  45. #    include    <DialogMgr.h>
  46. # else
  47. #    include    <WindowMgr.h>
  48. # endif
  49.  
  50. # include    <EventMgr.h>
  51. # include    <MenuMgr.h>
  52.  
  53.  
  54. # define    nil            0L
  55. # define    mBarHeight    20    /* menu bar height.  All window sizing
  56.                                code takes this into account */
  57.  
  58.  
  59. /*
  60.     This window zooming stuff may need to be removed if/when Think
  61.     supports it in the compiler.
  62. */
  63.  
  64. pascal short    TrackBox() = 0xa83b;    /* declare traps */
  65. pascal void        ZoomWindow() = 0xa83a;
  66.  
  67. enum                                    /* declare part codes */
  68. {
  69.     inZoomIn = 7,
  70.     inZoomOut
  71. };
  72.  
  73.  
  74.  
  75. /*
  76.     New(TypeName) returns handle to new object, for any TypeName.
  77.     If there is insufficient memory, the result is nil.
  78. */
  79.  
  80. # define    New(x)    (x **) NewHandle ((Size) sizeof (x))
  81.  
  82.  
  83. /*
  84.     Window and Menu handler types, constants, variables.
  85.  
  86.     whList and mhList are the lists of window and menu handlers.
  87.     whClobOnRmve and mhClobOnRmve are true if the handler disposal proc
  88.     is to be called when a handler is removed.  They are temporarily set
  89.     false when handlers are installed for windows or menus that already
  90.     have handlers - the old handler is removed WITHOUT calling the
  91.     disposal proc.
  92.  
  93.     Default lower limits on window sizing of 80 pixels both directions is
  94.     sufficient to allow text windows room to draw a grow box and scroll
  95.     bars without having the thumb and arrows overlap.  These values may
  96.     be changed if such a constraint is undesirable with SkelGrowBounds.
  97.     Default upper limits are for the Macintosh, not the Lisa, but are set
  98.     per machine in SkelInit.
  99. */
  100.  
  101. typedef struct WHandler
  102. {
  103.     WindowPtr    whWind;            /* window/dialog to be handled  */
  104.     ProcPtr        whClobber;        /* data structure disposal proc */
  105.     ProcPtr        whMouse;        /* mouse-click handler proc     */
  106.     ProcPtr        whKey;            /* key-click handler proc       */
  107.     ProcPtr        whUpdate;        /* update handler proc          */
  108.     ProcPtr        whActivate;        /* activate event handler proc  */
  109.     ProcPtr        whClose;        /* close "event" handler proc   */
  110.     ProcPtr        whIdle;            /* main loop proc               */
  111. # ifdef    supportDialogs
  112.     ProcPtr        whEvent;        /* event proc                   */
  113. # endif
  114.     Rect        whGrow;            /* limits on window sizing      */
  115.     Boolean        whSized;        /* true = window was resized    */
  116.     Boolean        whFrontOnly;    /* true = idle only when active */
  117.     struct WHandler    **whNext;    /* next window handler          */
  118. } WHandler;
  119.  
  120. static WHandler    **whList = nil;
  121. static Boolean    whClobOnRmve = true;
  122. static Rect        growRect = { 80, 80, 512, 342 - mBarHeight };
  123.  
  124.  
  125. typedef struct MHandler
  126. {
  127.     int                mhID;            /* menu id                     */
  128.     ProcPtr            mhSelect;        /* item selection handler proc */
  129.     ProcPtr            mhClobber;        /* menu disposal handler proc  */
  130.     struct MHandler    **mhNext;        /* next menu handler           */
  131. } MHandler;
  132.  
  133.  
  134. static MHandler    **mhList = nil;            /* list of menu handlers */
  135. static Boolean    mhClobOnRmve = true;
  136.  
  137.  
  138. /*
  139.     Variables for default Apple menu handler.  appleID is set to 1 if
  140.     SkelApple is called and is the id of the Apple menu, appleAboutProc
  141.     is the procedure to execute if there is an About... item and it's
  142.     chosen from the Apple menu.  If doAbout is true, then the menu
  143.     contains the About... item, otherwise it's just desk accessories.
  144. */
  145.  
  146. static MenuHandle    appleMenu;
  147. static int            appleID = 0;
  148. static ProcPtr        appleAboutProc = nil;
  149. static Boolean        doAbout = false;
  150.  
  151.  
  152. /*
  153.     Miscellaneous
  154.  
  155.     screenPort points to the window manager port.
  156.     
  157.     doneFlag determines when SkelMain returns.  It is set by calling
  158.     SkelWhoa(), which the host does to request a halt.
  159.  
  160.     pBkgnd points to a background procedure, to be run during event
  161.     processing.  Set it with SkelBackground.  If nil, there's no
  162.     procedure.
  163.  
  164.     pEvent points to an event-inspecting hook, to be run whenever an
  165.     event occurs.  Set it with SkelEventHook.  If nil, there's no
  166.     procedure.
  167.  
  168.     eventMask controls the event types requested in the GetNextEvent
  169.     call in SkelMain.
  170.  
  171.     diskInitPt is the location at which the disk initialization dialog
  172.     appears, if an uninitialized disk is inserted.
  173. */
  174.  
  175. static GrafPtr    screenPort;
  176. static int        doneFlag = false;
  177. static ProcPtr    pBkgnd = nil;
  178. static Boolean    (*pEvent)() = nil;
  179. static int        eventMask = everyEvent;
  180. static Point    diskInitPt = { /* v = */ 120, /* h = */ 100 };
  181.  
  182. # ifdef    supportDialogs
  183.  
  184. /*
  185.     Events that are passed to dialogs.  Others are ignored.
  186.     Standard mask passes , mousedown, keydown, autokey, update,
  187.     activate and null events.  Null events are controlled by bit 0.
  188. */
  189.  
  190. static int    dlogEventMask = 0x16b;
  191.  
  192. # endif
  193.  
  194.  
  195. /* -------------------------------------------------------------------- */
  196. /*                        Internal (private) Routines                        */
  197. /* -------------------------------------------------------------------- */
  198.  
  199.  
  200. /*
  201.     Get handler associated with user or dialog window.
  202.     Return nil if window doesn't belong to any known handler.
  203.     This routine is absolutely fundamental to TransSkel.
  204. */
  205.  
  206.  
  207. static WHandler **GetWDHandler (theWind)
  208. WindowPtr    theWind;
  209. {
  210. register WHandler    **h;
  211.  
  212.     for (h = whList; h != nil; h = (**h).whNext)
  213.     {
  214.         if ((**h).whWind == theWind)
  215.             return (h);
  216.     }
  217.     return (nil);
  218. }
  219.  
  220.  
  221. /*
  222.     Get handler associated with user window.
  223.     Return nil if window doesn't belong to any known handler.
  224.     The order of the two tests is critical:  theWind might be nil.
  225. */
  226.  
  227. static WHandler **GetWHandler (theWind)
  228. WindowPtr    theWind;
  229. {
  230. register WHandler    **h;
  231.  
  232.     if ((h = GetWDHandler (theWind)) != nil
  233.         && ((WindowPeek) theWind)->windowKind != dialogKind)
  234.     {
  235.             return (h);
  236.     }
  237.     return (nil);
  238. }
  239.  
  240.  
  241. # ifdef    supportDialogs
  242.  
  243. /*
  244.     Get handler associated with dialog window.
  245.     Return nil if window doesn't belong to any known handler.
  246.     The order of the two tests is critical:  theDialog might be nil.
  247. */
  248.  
  249. static WHandler **GetDHandler (theDialog)
  250. DialogPtr    theDialog;
  251. {
  252. register WHandler    **h;
  253.  
  254.     if ((h = GetWDHandler (theDialog)) != nil
  255.         && ((WindowPeek) theDialog)->windowKind == dialogKind)
  256.     {
  257.             return (h);
  258.     }
  259.     return (nil);
  260. }
  261.  
  262. # endif
  263.  
  264.  
  265. /*
  266.     General menu-handler.  Just passes selection to the handler's
  267.     select routine.  If the select routine is nil, selecting items from
  268.     the menu is a nop.
  269. */
  270.  
  271. static DoMenuCommand (command)
  272. long        command;
  273. {
  274. register int        menu;
  275. register int        item;
  276. register MHandler    **mh;
  277. register ProcPtr    p;
  278.  
  279.     menu = HiWord (command);
  280.     item = LoWord (command);
  281.     for (mh = mhList; mh != nil; mh = (**mh).mhNext)
  282.     {
  283.         if ((menu == (**mh).mhID) && ((p = (**mh).mhSelect) != nil))
  284.         {
  285.             (*p) (item);
  286.             break;
  287.         }
  288.     }
  289.     HiliteMenu (0);        /* command done, turn off menu hiliting */
  290. }
  291.  
  292.  
  293. /*
  294.     Apple menu handler
  295.     
  296.     DoAppleItem:  If the first item was chosen, and there's an "About..."
  297.     item, call the procedure associated with it (if not nil).  If there
  298.     is no "About..." item or the item was not the first one, then open
  299.     the associated desk accessory.  The port is saved and restored
  300.     because OpenDeskAcc does not always preserve it correctly.
  301.     
  302.     DoAppleClobber disposes of the Apple menu.
  303. */
  304.  
  305.  
  306. static DoAppleItem (item)
  307. int        item;
  308. {
  309. GrafPtr        curPort;
  310. Str255        str;
  311.  
  312.     if (doAbout && item == 1)
  313.     {
  314.         if (appleAboutProc != nil)
  315.             (*appleAboutProc) ();
  316.     }
  317.     else
  318.     {
  319.         GetPort (&curPort);
  320.         GetItem (appleMenu, item, str);        /* get DA name */
  321.         (void) OpenDeskAcc (str);            /* open it */
  322.         SetPort (curPort);
  323.     }
  324. }
  325.  
  326. static DoAppleClobber () { DisposeMenu (appleMenu); }
  327.  
  328.  
  329. /* -------------------------------------------------------------------- */
  330. /*                        Window-handler routing routines                    */
  331. /*                                                                        */
  332. /*    Each routine sets the port to the handler's window before executing    */
  333. /*    the handler procedure.                                                */
  334. /* -------------------------------------------------------------------- */
  335.  
  336.  
  337. /*
  338.     Pass local mouse coordinates, click time, and the modifiers flag
  339.     word to the handler.
  340. */
  341.  
  342. static DoMouse (h, theEvent)
  343. WHandler    **h;
  344. EventRecord    *theEvent;
  345.  
  346. {
  347. register ProcPtr    p;
  348. Point                thePt;
  349.  
  350.     if (h != nil)
  351.     {
  352.         SetPort ((**h).whWind);
  353.         if ((p = (**h).whMouse) != nil)
  354.         {
  355.             thePt = theEvent->where;    /* make local copy */
  356.             GlobalToLocal (&thePt);
  357.             (*p) (thePt, theEvent->when, theEvent->modifiers);
  358.         }
  359.     }
  360. }
  361.  
  362.  
  363. /*
  364.     Pass the character and the modifiers flag word to the handler.
  365. */
  366.  
  367. static DoKey (h, ch, mods)
  368. WHandler    **h;
  369. char        ch;
  370. int            mods;
  371. {
  372. register ProcPtr    p;
  373.  
  374.     if (h != nil)
  375.     {
  376.         SetPort ((**h).whWind);
  377.         if ((p = (**h).whKey) != nil)
  378.             (*p) (ch, mods);
  379.     }
  380. }
  381.  
  382.  
  383. /*
  384.     Call the window updating procedure, passing to it an indicator whether
  385.     the window has been resized or not.  Then clear the flag, assuming
  386.     the update proc took whatever action was necessary to respond to
  387.     resizing.
  388.  
  389.     If the handler doesn't have any update proc, the Begin/EndUpdate
  390.     stuff is still done, to clear the update region.  Otherwise the
  391.     Window Manager will keep generating update events for the window,
  392.     stalling updates of other windows.
  393.  
  394.     Make sure to save and restore the port, as it's not always the
  395.     active window that is updated.
  396. */
  397.  
  398. static DoUpdate (h)
  399. WHandler    **h;
  400.  
  401. {
  402. register WHandler    **rh;
  403. register ProcPtr    p;
  404. register GrafPtr    updPort;
  405. GrafPtr                tmpPort;
  406.  
  407.     if ((rh = h) != nil)
  408.     {
  409.         GetPort (&tmpPort);
  410.         SetPort (updPort = (**rh).whWind);
  411.         BeginUpdate (updPort);
  412.         if ((p = (**rh).whUpdate) != nil)
  413.         {
  414.             (*p) ((**rh).whSized);
  415.             (**rh).whSized = false;
  416.         }
  417.         EndUpdate (updPort);
  418.         SetPort (tmpPort);
  419.     }
  420. }
  421.  
  422.  
  423. /*
  424.     Pass activate/deactivate notification to handler.
  425. */
  426.  
  427. static DoActivate (h, active)
  428. WHandler    **h;
  429. Boolean        active;
  430.  
  431. {
  432. register ProcPtr    p;
  433.  
  434.     if ((h != nil) && ((p = (**h).whActivate) != nil))
  435.     {
  436.         SetPort ((**h).whWind);
  437.         (*p) (active);
  438.     }
  439. }
  440.  
  441.  
  442. /*
  443.     Execute a window handler's close proc.  This may be used by handlers
  444.     for temp windows that want to remove themselves when the window
  445.     is closed:  they can call SkelRmveWind to dispose of the window
  446.     and remove the handler from the window handler list.  Thus, windows
  447.     may be dynamically created and destroyed without filling up the
  448.     handler list with a bunch of invalid handlers.
  449.     
  450.     If the handler doesn't have a close proc, just hide the window.
  451.     The host should provide some way of reopening the window (perhaps
  452.     a menu selection).  Otherwise the window will be lost from user
  453.     control if it is hidden, since it won't receive user events.
  454.  
  455.     The port is set to the window manager port after calling the
  456.     handler proc, to avoid a dangling port.
  457.  
  458.     This is called both for regular and dialog windows.
  459. */
  460.  
  461. static DoClose (h)
  462. WHandler    **h;
  463. {
  464. register WHandler    **rh;
  465. register ProcPtr    p;
  466.  
  467.     if ((rh = h) != nil)
  468.     {
  469.         SetPort ((**rh).whWind);
  470.         if ((p = (**rh).whClose) != nil)
  471.             (*p) ();
  472.         else
  473.             HideWindow ((**rh).whWind);
  474.         SetPort (screenPort);
  475.     }
  476. }
  477.  
  478.  
  479. /*
  480.     Execute a window handler's clobber proc.
  481.  
  482.     The port is set to the window manager port after calling the
  483.     handler proc, to avoid a dangling port.
  484.  
  485.     This is called both for regular and dialog windows.
  486. */
  487.  
  488. static DoClobber (h)
  489. WHandler    **h;
  490. {
  491. register ProcPtr    p;
  492.  
  493.     if (h != nil)
  494.     {
  495.         SetPort ((**h).whWind);
  496.         if ((p = (**h).whClobber) != nil)
  497.             (*p) ();
  498.         SetPort (screenPort);
  499.     }
  500. }
  501.  
  502.  
  503. /*
  504.     Execute handler's idle proc.
  505.  
  506.     Make sure to save and restore the port, since idle procs may be
  507.     called for any window, not just the active one.
  508. */
  509.  
  510. static DoIdle (h)
  511. WHandler    **h;
  512. {
  513. register ProcPtr    p;
  514. GrafPtr                tmpPort;
  515.  
  516.     if (h != nil)
  517.     {
  518.         GetPort (&tmpPort);
  519.         SetPort ((**h).whWind);
  520.         if ((p = (**h).whIdle) != nil)
  521.             (*p) ();
  522.         SetPort (tmpPort);
  523.     }
  524. }
  525.  
  526.  
  527. # ifdef    supportDialogs
  528.  
  529. /* -------------------------------------------------------------------- */
  530. /*                            Dialog-handling routines                    */
  531. /* -------------------------------------------------------------------- */
  532.  
  533.  
  534. /*
  535.     Handle event if it's for a dialog.  The event must be one of
  536.     those that is passed to dialogs according to dlogEventMask.
  537.     This mask can be set so that disk-inserts, for instance, don't
  538.     get eaten up.
  539. */
  540.  
  541. static DoDialog (theEvent)
  542. EventRecord        *theEvent;
  543. {
  544. register WHandler    **dh;
  545. DialogPtr            theDialog;
  546. register int        what;
  547. int                    item;
  548. GrafPtr                tmpPort;
  549.  
  550. /*
  551.     handle command keys before they get to IsDialogEvent
  552. */
  553.  
  554.     what = theEvent->what;
  555.     if((what == keyDown || what == autoKey) && (theEvent->modifiers & cmdKey))
  556.     {
  557.            DoMenuCommand (MenuKey (theEvent->message & charCodeMask));
  558.            return (true);
  559.     }
  560.     
  561.     if(((1 << what) & dlogEventMask) && IsDialogEvent (theEvent))
  562.     {
  563.         
  564.         if (DialogSelect (theEvent, &theDialog, &item)
  565.            && (dh = GetDHandler (theDialog)) != nil
  566.            && (**dh).whEvent != nil)
  567.         {
  568.             GetPort (&tmpPort);
  569.             SetPort (theDialog);
  570.             (*(**dh).whEvent) (item, theEvent);
  571.             SetPort (tmpPort);
  572.         }
  573.         return (true);
  574.     }
  575.     return (false);
  576. }
  577.  
  578. # endif
  579.  
  580.  
  581. /* -------------------------------------------------------------------- */
  582. /*                            Event-handling routines                        */
  583. /* -------------------------------------------------------------------- */
  584.  
  585.  
  586. /*
  587.     Have either sized or zoomed the window.  Invalidate it to force
  588.     an update and set the 'resized' flag in the window handler true.
  589. */
  590.  
  591. static TriggerUpdate (h, thePort)
  592. WHandler    **h;
  593. GrafPtr        thePort;
  594. {
  595.     SetPort (thePort);
  596.     InvalRect (&thePort->portRect);
  597.     if (h != nil)
  598.         (**h).whSized = true;
  599. }
  600.  
  601.  
  602. /*
  603.     Size a window.  If the window has a handler, use the grow limits
  604.     in the handler record, otherwise use the defaults.
  605.  
  606.     The portRect is invalidated to force an update event.  (The port
  607.     must be set first, as it could be pointing anywhere.)  The handler's
  608.     update procedure should check the parameter passed to it to check
  609.     whether the window has changed size, if it needs to adjust itself to
  610.     the new size.  THIS IS A CONVENTION.  Update procs must notice grow
  611.     "events", there is no procedure specifically for such events.
  612.     
  613.     The clipping rectangle is not reset.  If the host application
  614.     keeps the clipping set equal to the portRect or something similar,
  615.     then it will have to arrange to treat window growing with more
  616.     care.
  617. */
  618.  
  619. static DoGrow (h, thePort, startPt)
  620. WHandler    **h;
  621. GrafPtr        thePort;
  622. Point        startPt;
  623. {
  624. Rect                r;
  625. register long        growRes;
  626.  
  627.     if (h != nil)
  628.         r = (**h).whGrow;
  629.     else
  630.         r = growRect;    /* use default */
  631.  
  632.     /* grow result non-zero if size change    */
  633.  
  634.     if (growRes = GrowWindow (thePort, startPt, &r))
  635.     {
  636.         SizeWindow (thePort, LoWord (growRes), HiWord (growRes), false);
  637.         TriggerUpdate (h, thePort);
  638.     }
  639. }
  640.  
  641.  
  642. /*
  643.     Zoom the current window.  Very similar to DoGrow
  644. */
  645.  
  646. DoZoom (h, thePort, partCode)
  647. register WHandler    **h;
  648. GrafPtr                thePort;
  649. short                partCode;
  650. {
  651.     ZoomWindow (thePort, partCode, 0);
  652.     TriggerUpdate (h, thePort);
  653. }
  654.  
  655.  
  656. /*
  657.     General event handler
  658. */
  659.  
  660. static DoEvent (theEvt)
  661. EventRecord    *theEvt;
  662.  
  663. {
  664. register EventRecord    *theEvent;
  665. Point                    evtPt;
  666. GrafPtr                    evtPort;
  667. register int            evtPart;
  668. register char            evtChar;
  669. register int            evtMods;
  670. register WHandler        **h;
  671. Rect                    r;
  672.  
  673.     theEvent = theEvt;
  674.  
  675. # ifdef    supportDialogs
  676.  
  677.     if(DoDialog (theEvent))
  678.         return;
  679.  
  680. # endif
  681.  
  682.     evtPt = theEvent->where;
  683.     switch (theEvent->what)
  684.     {
  685.  
  686.         case nullEvent:
  687.             break;
  688. /*
  689.     Mouse click.  Get the window that the click occurred in, and the
  690.     part of the window.
  691. */
  692.         case mouseDown:
  693.         {
  694.             evtPart = FindWindow (evtPt, &evtPort);
  695.             h = GetWHandler (evtPort);
  696.  
  697.             switch (evtPart)
  698.             {
  699. /*
  700.     Click in a desk accessory window.  Pass back to the system.
  701. */
  702.                 case inSysWindow:
  703.                 {
  704.                     SystemClick (theEvent, evtPort);
  705.                     break;
  706.                 }
  707. /*
  708.     Click in menu bar.  Track the mouse and execute selected command,
  709.     if any.
  710. */
  711.                 case inMenuBar:
  712.                 {
  713.                     DoMenuCommand (MenuSelect (evtPt));
  714.                     break;
  715.                 }
  716. /*
  717.     Click in grow box.  Resize window.
  718. */
  719.                 case inGrow:
  720.                 {
  721.                     DoGrow (h, evtPort, evtPt);
  722.                     break;
  723.                 }
  724. /*
  725.     Click in title bar.  Drag the window around.  Leave at least
  726.     4 pixels visible in both directions.
  727. */
  728.                 case inDrag:
  729.                 {
  730.                     r = screenPort->portRect;
  731.                     r.top += mBarHeight;            /* skip down past menu bar */
  732.                     InsetRect (&r, 4, 4);
  733.                     DragWindow (evtPort, evtPt, &r);
  734.                     break;
  735.                 }
  736. /*
  737.     Click in close box.  Call the close proc if the window has one.
  738. */
  739.                 case inGoAway:
  740.                 {
  741.                     if (TrackGoAway (evtPort, evtPt))
  742.                         DoClose (GetWDHandler (evtPort));
  743.                     break;
  744.                 }
  745. /*
  746.     Click in content region.  If the window wasn't frontmost (active),
  747.     just select it, otherwise pass the click to the window's mouse
  748.     click handler.
  749. */
  750.                 case inContent:
  751.                 {
  752.                     if (evtPort != FrontWindow ())
  753.                         SelectWindow (evtPort);
  754.                     else
  755.                         DoMouse (h, theEvent);
  756.                     break;
  757.                 }
  758.  
  759. /*
  760.     Click in zoom box.  Track the click and then zoom the window if
  761.     necessary
  762. */
  763.                 case inZoomIn:
  764.                 case inZoomOut:
  765.                 {
  766.                     if(TrackBox(evtPort, evtPt, evtPart))
  767.                         DoZoom (h, evtPort, evtPart);
  768.                     break;
  769.                 }
  770.  
  771.             }
  772.             break;    /* mouseDown */
  773.         }
  774. /*
  775.     Key event.  If the command key was down, process as menu item
  776.     selection, otherwise pass the character and the modifiers flags
  777.     to the active window's key handler.
  778.  
  779.     If dialogs are supported, there's no check for command-key
  780.     equivalents, since that would have been checked in DoDialog.
  781. */
  782.         case keyDown:
  783.         case autoKey:
  784.         {
  785.             evtChar = theEvent->message & charCodeMask;
  786.             evtMods = theEvent->modifiers;
  787.  
  788. # ifndef    supportDialogs
  789.  
  790.             if (evtMods & cmdKey)        /* try menu equivalent */
  791.             {
  792.                 DoMenuCommand (MenuKey (evtChar));
  793.                 break;
  794.             }
  795.  
  796. # endif
  797.  
  798.             DoKey (GetWHandler (FrontWindow ()), evtChar, evtMods);
  799.             break;
  800.         }
  801. /*
  802.     Update a window.
  803. */
  804.         case updateEvt:
  805.         {
  806.             DoUpdate (GetWHandler ((WindowPtr) theEvent->message));
  807.             break;
  808.         }
  809. /*
  810.     Activate or deactivate a window.
  811. */
  812.         case activateEvt:
  813.         {
  814.             DoActivate (GetWHandler ((WindowPtr) theEvent->message),
  815.                         ((theEvent->modifiers & activeFlag) != 0));
  816.             break;
  817.         }
  818. /*
  819.     handle inserts of uninitialized disks
  820. */
  821.         case diskEvt:
  822.         {
  823.             if (HiWord (theEvent->message) != noErr)
  824.             {
  825.                 DILoad ();
  826.                 (void) DIBadMount (diskInitPt, theEvent->message);
  827.                 DIUnload ();
  828.             }
  829.             break;
  830.         }
  831.     }
  832. }
  833.  
  834.  
  835. /* -------------------------------------------------------------------- */
  836. /*                        Interface (public) Routines                        */
  837. /* -------------------------------------------------------------------- */
  838.  
  839.  
  840. /*
  841.     Initialize the various Macintosh Managers.
  842.     Set default upper limits on window sizing.
  843.     FlushEvents does NOT toss disk insert events, so that disks
  844.     inserted while the application is starting up don't result
  845.     in dead drives.
  846. */
  847.  
  848. SkelInit ()
  849. {
  850.     MaxApplZone ();
  851.     FlushEvents (everyEvent - diskMask, 0 );
  852.     InitGraf (&thePort);
  853.     InitFonts ();
  854.     InitWindows ();
  855.     InitMenus ();
  856.     TEInit ();
  857.     InitDialogs (nil);        /* no restart proc */
  858.     InitCursor ();
  859. /*
  860.     Set upper limits of window sizing to machine screen size.  Allow
  861.     for the menu bar.
  862. */
  863.     GetWMgrPort (&screenPort);
  864.     growRect.right = screenPort->portRect.right;
  865.     growRect.bottom = screenPort->portRect.bottom - mBarHeight;
  866. }
  867.  
  868.  
  869. /*
  870.     Main loop.
  871.  
  872.     Task care of DA's with SystemTask.
  873.     Run background task if there is one.
  874.     If there is an event, check for an event hook.  If there isn't
  875.     one defined, or if there is but it returns false, call the
  876.     general event handler.  (Hook returns true if TransSkel should
  877.     ignore the event.)
  878.     If no event, call the "no-event" handler for the front window and for
  879.     any other windows with idle procedures that are always supposed
  880.     to run.  This is done in such a way that it is safe for idle procs
  881.     to remove the handler for their own window if they want (unlikely,
  882.     but...)  This loop doesn't check whether the window is really
  883.     a dialog window or not, but it doesn't have to, because such
  884.     things always have a nil idle proc.
  885.     
  886.     doneFlag is reset upon exit.  This allows it to be called
  887.     repeatedly, or recursively.
  888.  
  889.     If dialogs are supported, null events are looked at (in SkelMain)
  890.     and passed to the event handler.  This is necessary to make sure
  891.     DialogSelect gets called repeatedly, or the caret won't blink if
  892.     a dialog has any editText items.
  893.  
  894.     If an event-inspecting hook is installed, null events are not passed
  895.     to it.
  896. */
  897.  
  898. SkelMain ()
  899. {
  900. EventRecord            theEvent;
  901. register WHandler    **wh, **wh2;
  902. register WindowPtr    w;
  903. Boolean                haveEvent;
  904.  
  905.     while (!doneFlag)
  906.     {    
  907.         SystemTask ();
  908.         if (pBkgnd != nil)
  909.             (*pBkgnd) ();
  910.  
  911.         haveEvent = GetNextEvent (eventMask, &theEvent);
  912.  
  913.         if (pEvent == nil || (haveEvent && (*pEvent) (&theEvent) == false))
  914.             DoEvent(&theEvent);
  915.  
  916.         if (!haveEvent)
  917.         {
  918.             for (wh = whList; wh != nil; wh = wh2)
  919.             {
  920.                 wh2 = (**wh).whNext;
  921.                 w = (**wh).whWind;
  922.                 if ( (w == FrontWindow () || !(**wh).whFrontOnly ) )
  923.                 {
  924.                     SystemTask ();
  925.                     DoIdle (wh);
  926.                 }
  927.             }
  928.         }
  929.     }
  930.     doneFlag = false;
  931. }
  932.  
  933.  
  934. /*
  935.     Tell SkelMain to stop
  936. */
  937.  
  938. SkelWhoa () { doneFlag = true; }
  939.  
  940.  
  941. /*
  942.     Clobber all the menu, window and dialog handlers
  943. */
  944.  
  945. SkelClobber ()
  946. {
  947.     while (whList != nil)
  948.         SkelRmveWind ((**whList).whWind);
  949.  
  950.     while (mhList != nil)
  951.         SkelRmveMenu (GetMHandle((**mhList).mhID));
  952. }
  953.  
  954.  
  955. /* -------------------------------------------------------------------- */
  956. /*                        Menu-handler interface routines                    */
  957. /* -------------------------------------------------------------------- */
  958.  
  959.  
  960. /*
  961.     Install handler for a menu.  Remove any previous handler for it.
  962.     Pass the following parameters:
  963.  
  964.     theMenu    Handle to the menu to be handled.  Must be created by host.
  965.     pSelect    Proc that handles selection of items from menu.  If this is
  966.             nil, the menu is installed, but nothing happens when items
  967.             are selected from it.
  968.     pClobber Proc for disposal of handler's data structures.  Usually
  969.             nil for menus that remain in menu bar until program
  970.             termination.
  971.     
  972.     The menu is installed and drawn in the menu bar.
  973. */
  974.  
  975. SkelMenu (theMenu, pSelect, pClobber)
  976. MenuHandle    theMenu;
  977. ProcPtr        pSelect;
  978. ProcPtr        pClobber;
  979. {
  980. register MHandler    **mh;
  981.  
  982.     mhClobOnRmve = false;
  983.     SkelRmveMenu (theMenu);
  984.     mhClobOnRmve = true;
  985.  
  986.     mh = New (MHandler);
  987.     (**mh).mhNext = mhList;
  988.     mhList = mh;
  989.     (**mh).mhID = (**theMenu).menuID;    /* get menu id number */
  990.     (**mh).mhSelect = pSelect;            /* install selection handler */
  991.     (**mh).mhClobber = pClobber;        /* install disposal handler */
  992.     InsertMenu (theMenu, 0);            /* put menu at end of menu bar */
  993.     DrawMenuBar ();
  994. }
  995.  
  996.  
  997. /*
  998.     Remove a menu handler.  This calls the handler's disposal routine
  999.     and then takes the handler out of the handler list and disposes
  1000.     of it.
  1001.  
  1002.     Note that the menu MUST be deleted from the menu bar before calling
  1003.     the clobber proc, because the menu bar will end up filled with
  1004.     garbage if the menu was allocated with NewMenu (see discussion of
  1005.     DisposeMenu in Menu Manager section of Inside Macintosh).
  1006. */
  1007.  
  1008. SkelRmveMenu (theMenu)
  1009. MenuHandle    theMenu;
  1010. {
  1011. register int        mID;
  1012. register MHandler    **h, **h2;
  1013. register ProcPtr    p;
  1014.  
  1015.     mID = (**theMenu).menuID;
  1016.     if (mhList != nil)                /* if list empty, ignore */
  1017.     {
  1018.         if ((**mhList).mhID == mID)    /* is it the first element? */
  1019.         {
  1020.             h2 = mhList;
  1021.             mhList = (**mhList).mhNext;
  1022.         }
  1023.         else
  1024.         {
  1025.             for (h = mhList; h != nil; h = h2)
  1026.             {
  1027.                 h2 = (**h).mhNext;
  1028.                 if (h2 == nil)
  1029.                     return;                        /* menu not in list! */
  1030.                 if ((**h2).mhID == mID)            /* found it */
  1031.                 {
  1032.                     (**h).mhNext = (**h2).mhNext;
  1033.                     break;
  1034.                 }
  1035.             }
  1036.         }
  1037.         DeleteMenu (mID);
  1038.         DrawMenuBar ();
  1039.         if (mhClobOnRmve && (p = (**h2).mhClobber) != nil)
  1040.             (*p) (theMenu);                /* call disposal routine */
  1041.         DisposHandle (h2);                /* get rid of handler record */
  1042.     }
  1043. }
  1044.  
  1045.  
  1046. /*
  1047.     Install a handler for the Apple menu.
  1048.     
  1049.     SkelApple is called if TransSkel is supposed to handle the apple
  1050.     menu itself.  The title is the title of the first item.  If nil,
  1051.     then only desk accessories are put into the menu.  If not nil, then
  1052.     the title is entered as the first item, followed by a gray line,
  1053.     then the desk accessories.
  1054. */
  1055.  
  1056. SkelApple (aboutTitle, aboutProc)
  1057. StringPtr    aboutTitle;
  1058. ProcPtr        aboutProc;
  1059. {
  1060. Str255    appleTitle;
  1061.  
  1062.     appleTitle[0] = 1;        /* build apple menu title */
  1063.     appleTitle[1] = 0x14;    /* "apple" character */
  1064.     appleID = 1;
  1065.     appleMenu = NewMenu (appleID, appleTitle);
  1066.     if (aboutTitle != nil)
  1067.     {
  1068.         doAbout = true;
  1069.         AppendMenu (appleMenu, aboutTitle);    /* add About... item title */
  1070.         AppendMenu (appleMenu, "\p(-");        /* add gray line */
  1071.         appleAboutProc = aboutProc;
  1072.     }
  1073.     AddResMenu (appleMenu, 'DRVR');        /* add desk accessories */
  1074.     SkelMenu (appleMenu, DoAppleItem, DoAppleClobber);
  1075. }
  1076.  
  1077.  
  1078. /* -------------------------------------------------------------------- */
  1079. /*                    Window-handler interface routines                    */
  1080. /* -------------------------------------------------------------------- */
  1081.  
  1082.  
  1083. /*
  1084.     Install handler for a window.  Remove any previous handler for it.
  1085.     Pass the following parameters:
  1086.  
  1087.     theWind    Pointer to the window to be handled.  Must be created by host.
  1088.     pMouse    Proc to handle mouse clicks in window.  The proc will be
  1089.             passed the point (in local coordinates), the time of the
  1090.             click, and the modifier flags word.
  1091.     pKey    Proc to handle key clicks in window.  The proc will be passed
  1092.             the character and the modifier flags word.
  1093.     pUpdate    Proc for updating window.  TransSkel brackets calls to update
  1094.             procs with calls to BeginUpdate and EndUpdate, so the visRgn
  1095.             is set up correctly.  A flag is passed indicating whether the
  1096.             window was resized or not.  BY CONVENTION, the entire portRect
  1097.             is invalidated when the window is resized.  That way, the
  1098.             handler's update proc can redraw the entire content region
  1099.             without interference from BeginUpdate/EndUpdate.  The flag
  1100.             is set to false after the update proc is called; the
  1101.             assumption is made that it will notice the resizing and
  1102.             respond appropriately.
  1103.     pActivate Proc to execute when window is activated or deactivated.
  1104.             A boolean is passed to it which is true if the window is
  1105.             coming active, false if it's going inactive.
  1106.     pClose    Proc to execute when mouse clicked in close box.  Useful
  1107.             mainly to temp window handlers that want to know when to
  1108.             self-destruct (with SkelRmveWind).
  1109.     pClobber Proc for disposal of handler's data structures
  1110.     pIdle    Proc to execute when no events are pending.
  1111.     frontOnly True if pIdle should execute on no events only when
  1112.             theWind is frontmost, false if executes all the time.  Note
  1113.             that if it always goes, everything else may be slowed down!
  1114.  
  1115.     If a particular procedure is not needed (e.g., key events are
  1116.     not processed by a handler), pass nil in place of the appropriate
  1117.     procedure address.
  1118.  
  1119.     All handler procedures may assume that the port is set correctly
  1120.     at the time they are called.
  1121. */
  1122.  
  1123. SkelWindow (theWind, pMouse, pKey, pUpdate, pActivate, pClose,
  1124.                 pClobber, pIdle, frontOnly)
  1125.  
  1126. WindowPtr    theWind;
  1127. ProcPtr        pMouse, pKey, pUpdate, pActivate, pClose, pClobber, pIdle;
  1128. Boolean        frontOnly;
  1129. {
  1130. register WHandler    **hHand, *hPtr;
  1131.  
  1132.     whClobOnRmve = false;
  1133.     SkelRmveWind (theWind);
  1134.     whClobOnRmve = true;
  1135. /*
  1136.     Get new handler, attach to list of handlers.  It is attached to the
  1137.     beginning of the list, which is simpler; the order is presumably
  1138.     irrelevant to the host, anyway.
  1139. */
  1140.     hHand = New (WHandler);
  1141.     (**hHand).whNext = whList;
  1142.     whList = hHand;
  1143. /*
  1144.     Fill in handler fields
  1145. */
  1146.     hPtr = *hHand;
  1147.     hPtr->whWind = theWind;
  1148.     hPtr->whMouse = pMouse;
  1149.     hPtr->whKey = pKey;
  1150.     hPtr->whUpdate = pUpdate;
  1151.     hPtr->whActivate = pActivate;
  1152.     hPtr->whClose = pClose;
  1153.     hPtr->whClobber = pClobber;
  1154.     hPtr->whIdle = pIdle;
  1155.     hPtr->whFrontOnly = frontOnly;
  1156.     hPtr->whSized = false;
  1157.     hPtr->whGrow = growRect;
  1158.     SetPort (theWind);
  1159. }
  1160.  
  1161.  
  1162. /*
  1163.     Remove a window handler.  This calls the handler's disposal routine
  1164.     and then takes the handler out of the handler list and disposes
  1165.     of it.
  1166.  
  1167.     SkelRmveWind is also called by SkelRmveDlog.
  1168. */
  1169.  
  1170. SkelRmveWind (theWind)
  1171. WindowPtr    theWind;
  1172. {
  1173. register WHandler    **h, **h2;
  1174.  
  1175.     if (whList != nil)        /* if list empty, ignore */
  1176.     {
  1177.         if ((**whList).whWind == theWind)    /* is it the first element? */
  1178.         {
  1179.             h2 = whList;
  1180.             whList = (**whList).whNext;
  1181.         }
  1182.         else
  1183.         {
  1184.             for (h = whList; h != nil; h = h2)
  1185.             {
  1186.                 h2 = (**h).whNext;
  1187.                 if (h2 == nil)
  1188.                     return;                        /* theWind not in list! */
  1189.                 if ((**h2).whWind == theWind)    /* found it */
  1190.                 {
  1191.                     (**h).whNext = (**h2).whNext;
  1192.                     break;
  1193.                 }
  1194.             }
  1195.         }
  1196.         if (whClobOnRmve)
  1197.             DoClobber (h2);        /* call disposal routine */
  1198.         DisposHandle (h2);        /* get rid of handler record */
  1199.     }
  1200. }
  1201.  
  1202.  
  1203. # ifdef    supportDialogs
  1204.  
  1205. /* -------------------------------------------------------------------- */
  1206. /*                    Dialog-handler interface routines                    */
  1207. /* -------------------------------------------------------------------- */
  1208.  
  1209.  
  1210. /*
  1211.     Install a dialog handler.  Remove any previous handler for it.
  1212.     SkelDialog calls SkelWindow as a subsidiary to install a window
  1213.     handler, then sets the event procedure on return.
  1214.  
  1215.     Pass the following parameters:
  1216.  
  1217.     theDialog    Pointer to the dialog to be handled.  Must be created
  1218.             by host.
  1219.     pEvent    Event-handling proc for dialog events.
  1220.     pClose    Proc to execute when mouse clicked in close box.  Useful
  1221.             mainly to dialog handlers that want to know when to
  1222.             self-destruct (with SkelRmveDlog).
  1223.     pClobber Proc for disposal of handler's data structures
  1224.  
  1225.     If a particular procedure is not needed, pass nil in place of
  1226.     the appropriate procedure address.
  1227.  
  1228.     All handler procedures may assume that the port is set correctly
  1229.     at the time they are called.
  1230. */
  1231.  
  1232. SkelDialog (theDialog, pEvent, pClose, pClobber)
  1233. DialogPtr    theDialog;
  1234. ProcPtr        pEvent;
  1235. ProcPtr        pClose;
  1236. ProcPtr        pClobber;
  1237. {
  1238.     SkelWindow (theDialog, nil, nil, nil, nil, pClose, pClobber, nil, false);
  1239.     (**GetWDHandler (theDialog)).whEvent = pEvent;
  1240. }
  1241.  
  1242.  
  1243. /*
  1244.     Remove a dialog and its handler
  1245. */
  1246.  
  1247. SkelRmveDlog (theDialog)
  1248. DialogPtr    theDialog;
  1249. {
  1250.     SkelRmveWind (theDialog);
  1251. }
  1252.  
  1253. # endif
  1254.  
  1255.  
  1256. /* -------------------------------------------------------------------- */
  1257. /*                    Miscellaneous interface routines                    */
  1258. /* -------------------------------------------------------------------- */
  1259.  
  1260.  
  1261. /*
  1262.     Override the default sizing limits for a window, or, if theWind
  1263.     is nil, reset the default limits used by SkelWindow.
  1264. */
  1265.  
  1266. SkelGrowBounds (theWind, hLo, vLo, hHi, vHi)
  1267. WindowPtr    theWind;
  1268. int            hLo, vLo, hHi, vHi;
  1269. {
  1270. register WHandler    **h;
  1271. Rect                r;
  1272.  
  1273.     if (theWind == nil)
  1274.         SetRect (&growRect, hLo, vLo, hHi, vHi);
  1275.     else if ((h = GetWHandler (theWind)) != nil)
  1276.     {
  1277.         SetRect (&r, hLo, vLo, hHi, vHi);
  1278.         (**h).whGrow = r;
  1279.     }
  1280. }
  1281.  
  1282.  
  1283. /*
  1284.     Set the event mask.
  1285. */
  1286.  
  1287. SkelEventMask (mask)
  1288. int        mask;
  1289. {
  1290.     eventMask = mask;
  1291. }
  1292.  
  1293.  
  1294. /*
  1295.     Return the event mask.
  1296. */
  1297.  
  1298. SkelGetEventMask (mask)
  1299. int        *mask;
  1300. {
  1301.     *mask = eventMask;
  1302. }
  1303.  
  1304.  
  1305. /*
  1306.     Install a background task.  If p is nil, the current task is
  1307.     disabled.
  1308. */
  1309.  
  1310. SkelBackground (p)
  1311. ProcPtr    p;
  1312. {
  1313.     pBkgnd = p;
  1314. }
  1315.  
  1316.  
  1317. /*
  1318.     Return the current background task.  Return nil if none.
  1319. */
  1320.  
  1321. SkelGetBackground (p)
  1322. ProcPtr    *p;
  1323. {
  1324.     *p = pBkgnd;
  1325. }
  1326.  
  1327.  
  1328. /*
  1329.     Install an event-inspecting hook.  If p is nil, the hook is
  1330.     disabled.
  1331. */
  1332.  
  1333. SkelEventHook (p)
  1334. Boolean    (*p)();
  1335. {
  1336.     pEvent = p;
  1337. }
  1338.  
  1339.  
  1340. /*
  1341.     Return the current event-inspecting hook.  Return nil if none.
  1342. */
  1343.  
  1344. SkelGetEventHook (p)
  1345. Boolean    (**p)();
  1346. {
  1347.     *p = pEvent;
  1348. }
  1349.  
  1350.  
  1351. # ifdef    supportDialogs
  1352.  
  1353. /*
  1354.     Set the mask for event types that will be passed to dialogs.
  1355.     Bit 1 is always set, so that null events will be passed.
  1356. */
  1357.  
  1358. SkelDlogMask (mask)
  1359. int        mask;
  1360. {
  1361.     dlogEventMask = mask | 1;
  1362. }
  1363.  
  1364.  
  1365. /*
  1366.     Return the current dialog event mask.
  1367. */
  1368.  
  1369. SkelGetDlogMask (mask)
  1370. int        *mask;
  1371. {
  1372.     *mask = dlogEventMask;
  1373. }
  1374.  
  1375. # endif
  1376.